"""seed doctor dictionary options

Revision ID: 4a7d3e8a9132
Revises: 3b2f55ffb2f2
Create Date: 2025-10-24 16:40:00.000000

"""
import uuid
from datetime import datetime

from alembic import op
import sqlalchemy as sa
from sqlalchemy import text


# revision identifiers, used by Alembic.
revision = '4a7d3e8a9132'
down_revision = '3b2f55ffb2f2'
branch_labels = None
depends_on = None


DOCTOR_DICT_TYPES = [
    {
        "dict_type": "doctor_hospital",
        "dict_name": "医疗机构",
        "remark": "医生注册可选的医疗机构",
        "options": [
            (1, "北京协和医院", "peking_union"),
            (2, "上海瑞金医院", "ruijin_hospital"),
            (3, "广州中山医院", "zhongshan_hospital"),
        ],
    },
    {
        "dict_type": "doctor_department",
        "dict_name": "科室",
        "remark": "医生注册可选的科室",
        "options": [
            (1, "心内科", "cardiology"),
            (2, "神经内科", "neurology"),
            (3, "内分泌科", "endocrinology"),
        ],
    },
    {
        "dict_type": "doctor_title",
        "dict_name": "职称",
        "remark": "医生注册可选的职称",
        "options": [
            (1, "主任医师", "chief_physician"),
            (2, "副主任医师", "associate_chief"),
            (3, "主治医师", "attending_physician"),
        ],
    },
]


def _get_current_timestamp() -> datetime:
    return datetime.utcnow()


def upgrade() -> None:
    bind = op.get_bind()

    for dict_group in DOCTOR_DICT_TYPES:
        dict_type = dict_group["dict_type"]
        dict_name = dict_group["dict_name"]
        remark = dict_group.get("remark")

        result = bind.execute(
            text("SELECT id FROM sys_dict_type WHERE dict_type = :dict_type"),
            {"dict_type": dict_type},
        )
        existing_id = result.scalar_one_or_none()

        timestamp = _get_current_timestamp()

        if existing_id is None:
            dict_type_id = uuid.uuid4()
            bind.execute(
                text(
                    """
                    INSERT INTO sys_dict_type
                        (id, dict_name, dict_type, status, remark, create_by, update_by, created_at, updated_at)
                    VALUES
                        (:id, :dict_name, :dict_type, '0', :remark, 'system', 'system', :created_at, :updated_at)
                    """
                ),
                {
                    "id": str(dict_type_id),
                    "dict_name": dict_name,
                    "dict_type": dict_type,
                    "remark": remark,
                    "created_at": timestamp,
                    "updated_at": timestamp,
                },
            )
        else:
            dict_type_id = existing_id

        for sort, label, value in dict_group.get("options", []):
            exists = bind.execute(
                text(
                    "SELECT 1 FROM sys_dict_data WHERE dict_type = :dict_type AND dict_value = :dict_value"
                ),
                {"dict_type": dict_type, "dict_value": value},
            ).scalar_one_or_none()

            if exists:
                continue

            bind.execute(
                text(
                    """
                    INSERT INTO sys_dict_data
                        (id, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default,
                         status, remark, create_by, update_by, dict_type_id, created_at, updated_at)
                    VALUES
                        (:id, :dict_sort, :dict_label, :dict_value, :dict_type, NULL, NULL, :is_default,
                         '0', NULL, 'system', 'system', :dict_type_id, :created_at, :updated_at)
                    """
                ),
                {
                    "id": str(uuid.uuid4()),
                    "dict_sort": sort,
                    "dict_label": label,
                    "dict_value": value,
                    "dict_type": dict_type,
                    "is_default": "Y" if sort == 1 else "N",
                    "dict_type_id": str(dict_type_id),
                    "created_at": timestamp,
                    "updated_at": timestamp,
                },
            )


def downgrade() -> None:
    bind = op.get_bind()

    for group in DOCTOR_DICT_TYPES:
        dict_type = group["dict_type"]
        bind.execute(
            text("DELETE FROM sys_dict_data WHERE dict_type = :dict_type"),
            {"dict_type": dict_type},
        )
        bind.execute(
            text("DELETE FROM sys_dict_type WHERE dict_type = :dict_type"),
            {"dict_type": dict_type},
        )
